import 'package:flutter/material.dart';
import 'package:znmm/config/app_color.dart';

import '../mag/category_mag.dart';
import '../model/category_model.dart';

class CategoryLeftView extends StatefulWidget {
  final Function() onSelCallBack;

  const CategoryLeftView({super.key, required this.onSelCallBack});

  @override
  State<CategoryLeftView> createState() => _CategoryLeftViewState();
}

class _CategoryLeftViewState extends State<CategoryLeftView> {
  late List<CategoryModel> _pModelList = [];

  @override
  void initState() {
    super.initState();

    _pModelList = CategoryMag().getCategoryList();
  }

  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.centerLeft, // 左侧对齐
      child: Material(
        //type: MaterialType.transparency,
        color: AppColor.backgroundColor,
        borderRadius: const BorderRadius.only(
          topRight: Radius.circular(16.0),
          bottomRight: Radius.circular(16.0),
        ),
        child: SizedBox(
          width: MediaQuery.of(context).size.width * 0.6, // 占据屏幕一半宽度
          height: MediaQuery.of(context).size.height, // 高度占满屏幕
          child: ListView.builder(
            itemCount: _pModelList.length,
            itemBuilder: (BuildContext context, int index) {
              CategoryModel pModel = _pModelList[index];
              bool bIsTitle = pModel.bIsTitle;

              bool bSel =
                  CategoryMag().categorySlug == pModel.slug ? true : false;

              return ListTile(
                title: Text(pModel.name,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: bIsTitle ? 18 : 16,
                        color: bSel ? AppColor.blue : AppColor.black,
                        fontWeight: bIsTitle ? FontWeight.bold : null)),
                onTap: () {
                  setState(() {
                    widget.onSelCallBack();

                    String slugStr = pModel.slug;
                    CategoryMag().reqProductByCateSlugGraql(slugStr);
                    Navigator.of(context).pop();
                  });
                },
                //selected: subModel.name,
              );
            },
          ),
        ),
      ),
    );
  }
}
